import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

class CookUser extends StatefulWidget {
  CookUser({Key key, this.nickname, this.cover, this.desc, this.id})
      : super(key: key);
  final String nickname;
  String desc;
  String cover;
  int id;
  @override
  _CookUserState createState() =>
      new _CookUserState(this.nickname, this.desc, this.cover, this.id);
}

class _CookUserState extends State<CookUser> {
  String nickname;
  String desc;
  String cover;
  int id;
  _CookUserState(this.nickname, this.desc, this.cover, this.id);

  Dio dio = Dio();
  bool isFavorite = true;
  Color fontColor = Color.fromRGBO(255, 75, 54, 1);
  Color fontUnselectColor = Color.fromRGBO(200, 200, 200, 1);

  _onSelectedIndex() async {
    if (isFavorite) {
      final String url = 'http://127.0.0.1:7001/favorite/cancel';
      final res = await dio.post(url, data: {
        'user_id': 1,
        'favorite_type': 2,
        'favorite_user_id': this.id
      });
      isFavorite = false;
    } else {
      final String url = 'http://127.0.0.1:7001/favorite/setFavoriteCount';
      final res = await dio.post(url, data: {
        'user_id': 1,
        'favorite_type': 2,
        'favorite_user_id': this.id
      });
      isFavorite = true;
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        child: Container(
          height: 80,
          margin: const EdgeInsets.fromLTRB(20, 10, 20, 10),
          padding: const EdgeInsets.fromLTRB(0, 10, 0, 10),
          decoration: BoxDecoration(
              color: Colors.white38,
              boxShadow: [
                BoxShadow(
                    color: Colors.white,
                    offset: Offset(0.0, 0), //阴影xy轴偏移量
                    blurRadius: 1.0, //阴影模糊程度
                    spreadRadius: 1.0 //阴影扩散程度
                    )
              ],
              border:
                  Border(bottom: BorderSide(width: 1, color: Colors.black12))),
          child: Flex(
            direction: Axis.horizontal,
            children: [
              Expanded(
                  flex: 0,
                  child: Padding(
                      padding: const EdgeInsets.fromLTRB(0, 0, 20, 0),
                      child: ClipOval(
                        child: Image(
                          image: NetworkImage(this.cover),
                          fit: BoxFit.cover,
                          width: 60,
                          height: 60,
                        ),
                      ))),
              Expanded(
                  flex: 3,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        this.nickname,
                        textAlign: TextAlign.left,
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                          color: Colors.black,
                          fontSize: 16.0,
                          fontWeight: FontWeight.w500,
                          height: 1.2,
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.fromLTRB(0, 10, 0, 10),
                        child: Text(
                          this.desc,
                          textAlign: TextAlign.left,
                          maxLines: 1,
                          style: TextStyle(
                              fontSize: 13.0, color: Colors.grey, height: 1.0),
                        ),
                      )
                    ],
                  )),
              Expanded(
                child: IconButton(
                  icon: Icon(isFavorite == true
                      ? Icons.favorite
                      : Icons.favorite_border),
                  color: isFavorite == true ? fontColor : fontUnselectColor,
                  onPressed: () {
                    _onSelectedIndex();
                  },
                ),
              )
            ],
          ),
        ),
        onTap: () => updateText(this.nickname));
  }

  // 选项点击
  void updateText(String s) async {}

  // 弹出对话框
  Future<bool> showDeleteConfirmDialog1() {
    return showDialog<bool>(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text("提示"),
          content: Text("当前已是最新版本?"),
          actions: <Widget>[
            FlatButton(
              child: Text("取消"),
              onPressed: () => Navigator.of(context).pop(), // 关闭对话框
            ),
            FlatButton(
              child: Text("确认"),
              onPressed: () {
                //关闭对话框并返回true
                Navigator.of(context).pop(true);
              },
            ),
          ],
        );
      },
    );
  }
}
